function cellNames=fieldnamesReplace(struct,namesStruct) 
% function cellNames=fieldnamesReplace(struct,namesStruct) 
%  
% struc         Cell or Structure 
% namesStruc    Structure with subfields of struc that must be replaced
%               Can have 1 or two columns 
% 
% Crates cellNames which replaces the fieldnames of struct with those
% indicated in namesStruct and adds a description if any of the fields of
% namesStruct has 2 dimensions 
% 
% Use to crates names for output 
% Alejandro Justiniano Feb 2013
%==========================================================================
if isstruct(struct)==false 
    if iscell(struct)==false 
        error('First input must be a structure or cell'); 
    else 
        struct=cell2struct(struct,struct); 
        disp('Converted cell to structure'); 
    end 
end 
if isstruct(namesStruct)==false 
    error('namesStruct must be a structure') 
end 
fields=fieldnames(struct); 
Nfields=length(fields); 

fieldsReplace=fieldnames(namesStruct); 
Nreplace=length(fieldsReplace); 

%% Determine whether there is 1 or two columns to create 
maxDim=0; 
for ii=1:Nreplace; 
    fldnm=fieldsReplace{ii};
    flagCell=iscell( namesStruct.(fldnm) );
    flagChar= ischar( namesStruct.(fldnm) );
    if flagCell== false && flagChar==false
        error(['Fieldname ',fldnm,' must be cell or characters']);
    end
    if flagChar==true 
        currDim=1; 
    else
        currDim=length( namesStruct.(fldnm) ); 
    end 
    maxDim=max( currDim, maxDim ); 
end 
    
cellNames=cell(Nfields,maxDim); 

for ii=1:Nfields
    fldnm=fields{ii};
    % Is there a match?
    pos=strcmp(fldnm,fieldsReplace);
    % No 
    if all(pos==0);
        cellNames{ii,1}=fldnm;
        if maxDim==2
            cellNames{ii,2}=fldnm;
        end
    else
        % Yes
        if  flagCell==true;
            cellNames(ii,1)=namesStruct.(fldnm)(1);
        else
            cellNames{ii,1}=namesStruct.(fldnm);
        end
        if maxDim==2
            cellNames(ii,2)=namesStruct.(fldnm)(2);
        end
    end
end 